#pragma once

#include "socketWin.h"
#include "socketLiunx.h"

#include "packet.h"
#include "handler.h"

class GetSocket
{
public:

	static Socket* SocketInstance(SocketType socketType);
};

enum HANDLE_PACKET_ERROR
{
	HANDLE_PACKET_NONE = 0,
	HANDLE_PACKET_INVAILD = -1,
	HANDLE_PACKET_NOREG_OPCODE = -2,
	HANDLE_PACKET_ERROR = -3,
	HANDLE_PACKET_FORMAT_DONOT_MATCH = -4
};

class Connection
{
public:

	Connection(SocketType socketType, int socket = -1);
	virtual ~Connection();
	
	bool IsAlive();
	void Disconnect();

	virtual int Update() = 0;
	virtual void HandleSendSocket() = 0;
	virtual void HandleRecvSocket() = 0;
	virtual void ClosePacket() = 0;
	virtual bool Accept(){ return true; }

	bool Recv();
	void RecvSelect();
	
	bool Send(const string& data);
	bool Send(const char* buffer, size_t size);
	bool SendSelect(const string& data);
	bool SendSelect(const char* buffer, size_t size);

	void SetUid(string uid) { m_uid = uid; }
	inline string GetUid() const { return m_uid; }

	void SetPassword(string password) { m_password = password; }
	inline string GetPassword() const { return m_password; }
	
	inline bool recv_select() { return IsAlive() && m_socket->Select(SOCKET_SELECT_READ); }
	inline bool send_select() { return IsAlive() && m_socket->Select(SOCKET_SELECT_WRITE);}
	
	inline bool GetInSocketSignal()	 { return m_bIsAliveInSocket; }
	inline void SetInSocketSignal()	 { m_bIsAliveInSocket = false; }

	Socket* GetSocket()				 { return m_socket; }

protected:

	Socket*		m_socket;
	Mutex		m_mutex;
	string		m_recvBuffer;
	string		m_uid;
	string		m_password;

	PakcetQueue		m_sendQueue;
	PakcetQueue		m_recvQueue;

	bool	m_bIsAliveInSocket;
};